import path from 'path'
import fs from 'fs'
const remote = require('@electron/remote')
const userPath = path.join(remote.app.getPath('userData'), '/user.json')
const groupPath = path.join(remote.app.getPath('userData'), '/group.json')

console.log(userPath)
console.log(groupPath)

export const keys = {
  ACCOUNT_LIST: 'ACCOUNT_LIST',
  WEBSITE: 'WEBSITE'
}

export const getCurWebsite = () => {
  // return window.localStorage.getItem(keys.WEBSITE) || 'http://45.124.112.137:3000'
  // return 'http://127.0.0.1:3100'
  return 'http://45.147.26.157:3100'
  // return 'http://45.124.112.137:3000'
  // return 'http://176.113.69.25:3100'
}

export const setCurWebsite = (val) => {
  window.localStorage.setItem(keys.WEBSITE, val)
}

export const baseUrl = getCurWebsite()

export const toDataNumbers = (data) => {
  if (typeof data !== 'number' && !data) {
    console.error('toDataNumbers error')
    return
  }
  let result
  if (Array.isArray(data)) {
    result = []
    for (let i = 0; i < data.length; i++) {
      result.push(toDataNumbers(data[i]))
    }
  } else if (Object.prototype.toString.call(data) === '[object Object]') {
    result = {}
    Object.keys(data).forEach(key => {
      result[key] = toDataNumbers(data[key])
    })
  } else {
    if (typeof data === 'number' || Number(data)) {
      const n = Number(data).toFixed(2) + ''
      result = (n + '').replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ''
    }
  }
  return result || data
}

export const levelList = [
  {
    code: 0,
    message: '等级零'
  },
  {
    code: 1,
    message: '等级一'
  },
  {
    code: 2,
    message: '等级二'
  },
  {
    code: 3,
    message: '等级三'
  }
]

export const platformList = [
  {
    code: 1,
    message: '皇冠'
  },
  {
    code: 2,
    message: '沙巴'
  },
  {
    code: 3,
    message: '智博'
  },
  {
    code: 4,
    message: '利己'
  },
  {
    code: 5,
    message: '小八'
  },
  {
    code: 6,
    message: 'CC'
  },
  {
    code: 7,
    message: '平博'
  },
  {
    code: 8,
    message: '永盈'
  },
  {
    code: 9,
    message: '雄图'
  }
  // {
  //   code: 8,
  //   message: '小白'
  // }
]

export const getLevelMessage = (code, defaultMsg) => {
  const item = levelList.find(e => e.code === code)
  if (item) {
    return item.message
  }
  return defaultMsg || ''
}

export const getAccountListAll = () => {
  // const data = window.localStorage.getItem(keys.ACCOUNT_LIST)
  // if (data) {
  //   return JSON.parse(data) || {}
  // }

  const fileData = getUserFileData()
  if (fileData) {
    return fileData
  }

  return {}
}

export const setAccountListAll = (allList) => {
  const result = JSON.stringify(allList)
  setUserFileData(result)
}

export const getAccountList = (key) => {
  // let data = window.localStorage.getItem(keys.ACCOUNT_LIST)
  const fileData = getUserFileData()
  if (fileData) {
    return fileData[key] || []
  }

  return []
  // data = data ? JSON.parse(data) : {}
  // return data[key] || []
}

export const setAccountList = (key, data = []) => {
  const allList = getAccountListAll()
  allList[key] = data
  const result = JSON.stringify(allList)
  // window.localStorage.setItem(keys.ACCOUNT_LIST, result)
  setUserFileData(result)
}

export const floatAccountList = (data) => {
  const list = []
  const _help = (arr) => {
    for (let i = 0; i < arr.length; i++) {
      const item = arr[i]
      list.push(item)
      if (item && item.children && item.children.length) {
        _help(item.children)
      }
    }
  }

  _help(data)

  return list
}

export const setUserFileData = (data) => {
  try {
    fs.writeFileSync(userPath, data)
  } catch (e) {
    console.log('getUserFileData Error')
    console.log(e)
  }
}

export const getUserFileData = () => {
  try {
    const data = fs.readFileSync(userPath, 'utf-8')
    if (data) {
      return JSON.parse(data)
    }
  } catch (e) {
    console.log('getUserFileData Error')
    console.log(e)
  }
  return null
}
